﻿using System;
using System.Data.Entity;
using System.Web.Security;
using Abmemunit.Data.Infrastructure;
using Abmemunit.Domain;
using Abmemunit.Domain.Users;
using Abmemunit.Data.Mapping.Users;


namespace Abmemunit.Data
{
    public class AbmemunitContext : DbContext,IDbContext
    {
        
        public DbSet<User> Users { get; set; }
        public DbSet<UserRole> Roles { get; set; }


        public AbmemunitContext()
        {
            Database.SetInitializer(new AbmemunitContextInitializer());
        }
        public virtual void Commit()
        {
            base.SaveChanges();
        }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new UserMap());
            modelBuilder.Configurations.Add(new UserRoleMap());
        }
        public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
        {
            return base.Set<TEntity>();
        }
    }

    public class AbmemunitContextInitializer : DropCreateDatabaseIfModelChanges<AbmemunitContext>
    {
        private const string Passwordformate = "SHA1";
        protected override void Seed(AbmemunitContext context)
        {

            var crAdmin =
                new UserRole
                    {
                        Name = UserRoleNames.Administrators,
                        Active = true,
                    };


            var crMember =
                new UserRole
                    {
                        Name = UserRoleNames.Members,
                        Active = true,
                    };

            context.Roles.Add(crAdmin);
            context.Roles.Add(crMember);

            const string password = "1234567";
            string defaultUserPassword = GetPassword(password);

            var adAdminMember =
                new User()
                    {
                        Username = "abmem",
                        Email = "abmem@test.com",
                        MailingAddress = "Dhaka,Bangladesh",
                        Password = defaultUserPassword,
                        Active = true,
                        IsAdminAccount = true,
                        CreatedOnUtc = DateTime.UtcNow

                    };
            adAdminMember.UserRoles.Add(crAdmin);
            context.Users.Add(adAdminMember);

            var adMember =
                new User()
                    {
                        Username = "abc",
                        Email = "abc@test.com",
                        MailingAddress = "Dhaka,Bangladesh",
                        Password = defaultUserPassword,
                        Active = true,
                        IsAdminAccount = false,
                        CreatedOnUtc = DateTime.UtcNow
                    };

            adMember.UserRoles.Add(crAdmin);
            context.Users.Add(adMember);
            context.SaveChanges();

        }

        private string GetPassword(string rwPassword)
        {
            const string passwordSaltTemp = "";

            string saltAndPassword = string.Concat(rwPassword, passwordSaltTemp);
            string formattedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(
                saltAndPassword, Passwordformate);
            return formattedPassword;
        }
    }
}
